#!/bin/bash
#
counter=0
doublefailure=0
fail=0
flag=searchResultTime
i=0


################modify the start point##############
start=0

###########modify the file name before executing####
for line in `cat AND.txt`;
do 
keywords[i]=$line
let "i+=1"
done

con=${#keywords[@]}
let "con=con-$start"

while [[ $con -gt 0 ]]
do
let "con-=1"

keyword=${keywords[$start]}


#result=$(curl -e http://www.my-ajax-site.com 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='$keyword |fgrep -c $keyword)
output=$(curl -e http://www.my-ajax-site.com 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='$keyword'&rsz=large')
result=$(echo $output | fgrep -c $flag)

if [[ $result -gt 0 ]]; then
let "counter+=1"
let "doublefailure=0"
echo $output',' >> output.txt
echo start: $start'; keyword: '$keyword >> output.log 
let "start+=1"
else 

echo $output',' >> errer.output
echo start: $start'; keyword: '$keyword '-----error'>> output.log 
let "fail+=1"

if [[ $doublefailure -gt 0 ]]; then
sleep 300s
fi

if [[ $doublefailure -gt 1 ]]; then
sleep 300s
fi

if [[ $doublefailure -gt 2 ]]; then
sleep 300s
fi

if [[ $doublefailure -gt 3 ]]; then
sleep 900s
fi

if [[ $doublefailure -gt 4 ]]; then
sleep 1800s
fi

let "doublefailure+=1"

#sleep 45s
fi



echo =========================================================
echo keyword: $keyword, counter: $counter, fail: $fail, doublefail: $doublefailure
echo =========================================================

random=$RANDOM
let "random=random%90+30"
sleep "$random"s


done
